package com.dao.imp;

import com.dao.LoginDao;
import com.util.DBConn;

import java.sql.ResultSet;
import com.entity.User;

public class LoginDaoImp implements LoginDao {
    private boolean loginResult = false;
    private User user = null;
    public User getUser(){
        return this.user;
    }

    public boolean isLoginResult() {
        return loginResult;
    }

    @Override
    public boolean login(String userId, String password){
//        username会匹配到email或nickname
        String sql = "select distinct * from Users where (user_email='" + userId +"'and user_password='"+password+ "')or(user_id='"+userId+"' and user_password='"+password+"')";
//        System.out.println("sql:" + sql);
        tryLogin(sql);
        return this.loginResult;
        
    }
    @Override
    public User loginResult(String username, String password){
        login(username,password);
        return this.user;
    }

    @Override
    public User getUserInfo(String userId){
        String sql = "select distinct * from Users where user_id='" + userId +"')";
//        System.out.println(sql);
        tryLogin(sql);
        return this.user;
    }

    private void tryLogin(String sql) {
        User user=new User();

        try {
            DBConn.init();
            ResultSet rs = DBConn.selectSql(sql);
            while (rs.next()) {
                user.setUser_id(rs.getInt("user_id"));
                user.setUser_email(rs.getString("user_email"));
                user.setUser_nickname(rs.getString("user_nickname"));
                user.setUser_password(rs.getString("user_password"));
            }
            DBConn.closeConn();
        } catch (Exception e) {
            //
        }
//        System.out.println(user.getUser_id());
        if(user!=null){
            this.loginResult=true;
        }
        this.user=user;

    }






}
